package com.huibo.aigou.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.huibo.aigou.base.bo.AjaxResult;
import com.huibo.aigou.client.RedisClient;
import com.huibo.aigou.client.VelocityClient;
import com.huibo.aigou.domain.ProductType;
import com.huibo.aigou.mapper.ProductTypeMapper;
import com.huibo.aigou.service.ProductTypeService;
import com.netflix.discovery.converters.Auto;

@Service
public class ProductTypeServiceImpl extends ServiceImpl<ProductTypeMapper, ProductType> implements ProductTypeService{

	private ObjectMapper objectMapper = new ObjectMapper();
	
	@Autowired
	private RedisClient redisClient;
	
	@Autowired
	private VelocityClient velocityClient;
	
	@Override
	public List<ProductType> selectTree() {
		return selectMapTree();
	}
	
	@SuppressWarnings("unchecked")
	private List<ProductType> selectMapTree() {
		String value = redisClient.get("productTypes");
		if(value == null || value.trim().equals("")) {
			System.out.println("数据库读取");
			List<ProductType> productTypes = list();
			List<ProductType> pTypes = new ArrayList<>();
			Map<Long, ProductType> maps = new HashMap<Long, ProductType>();
			for(ProductType pt : productTypes) {
				maps.put(pt.getId(), pt);
			}
			for(ProductType pt : productTypes) {
				if(pt.getPid() == 0) pTypes.add(pt);
				else {
					maps.get(pt.getPid()).getProductTypes().add(pt);
				}
			}
			try {
				value = objectMapper.writeValueAsString(pTypes);
				redisClient.set("productTypes", value);
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}
			return pTypes;
		}
		System.out.println("redis读取");
		List<ProductType> pTypes = new ArrayList<>();
		try {
			pTypes = objectMapper.readValue(value, pTypes.getClass());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return pTypes;
	}

	@Override
	public AjaxResult writePageHome() {
		String templatePath = "E:\\stsworkspace\\aigou-parent\\aigou-product\\product-service\\src\\main\\resources\\template\\home\\product.type.vm";
		String tagetPath = "E:\\stsworkspace\\aigou-parent\\aigou-product\\product-service\\src\\main\\resources\\template\\product.type.vm.html";
		List<ProductType> types = selectMapTree();
        velocityClient.write(templatePath, tagetPath, types);
        templatePath = "E:\\stsworkspace\\aigou-parent\\aigou-product\\product-service\\src\\main\\resources\\template\\home.vm";
        tagetPath = "C:\\Users\\acer\\Documents\\HBuilderProjects\\aigou-home\\home.html";
        Map<String, Object> maps = new HashMap<>();
        maps.put("staticRoot", "E:\\stsworkspace\\aigou-parent\\aigou-product\\product-service\\src\\main\\resources\\");
        return velocityClient.write(templatePath, tagetPath, maps);
	}
	
	/*private List<ProductType> selectForTree() {
		List<ProductType> productTypes = list();
		List<ProductType> pTypes = new ArrayList<>();
		for(ProductType pt : productTypes) {
			if(pt.getPid() == 0) {
				pTypes.add(pt);
			}
			List<ProductType> pts = new ArrayList<ProductType>();
			for(ProductType pty : productTypes) {
				if(pty.getPid() == pt.getId())
					pts.add(pty);
			}
			pt.setProductTypes(pts);
		}
		return pTypes;
	}
	
	private List<ProductType> selectPidTree(Long pid) {
		List<ProductType> productTypes = baseMapper.selectList(new QueryWrapper<ProductType>().eq("pid", pid));
		if(productTypes == null || productTypes.size() <= 0) {
			return null;
		}
		for(ProductType pt : productTypes) {
			List<ProductType> childPros = selectPidTree(pt.getId());
			pt.setProductTypes(childPros);
		}
		return productTypes;
	}*/

}
